package Hangman;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javazoom.jl.decoder.JavaLayerException;
/**
 * 
 * The Sound class, used for playing the game sounds.
 * @author Group 1; Robin E, Erik P, Cristian O, Jacob L, Tobias W, Isac A.
 */
public class Sound implements Runnable{
	
	private Thread thread;
	private String file;
	private Boolean active;
	private Boolean playSong;
	private javazoom.jl.player.Player clip;
	
	public Sound(String _file){
		thread= new Thread(this, "soundThread");
		file= _file;
		active= true;
		playSong= true;
		if(_file!=null)
			thread.start();
	}
	
	
/**
 * playSound, Method for playing sound files.
 * @param file which filename to play.
 */
	synchronized public void playSound() {
		try {
			FileInputStream audio =new FileInputStream(new File(file));
			clip = new javazoom.jl.player.Player(audio);
			clip.play();
			
			}

		catch (JavaLayerException e1) {
			System.out.println(e1);
		} catch (IOException ioe) {
			System.out.println(ioe);
		}
	}

@Override
	public void run() {
	
	while(active){
		if(playSong==true)
			playSound();
		else {
			try {
				thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			
	}
}

public void pauseSong(){
	playSong= false;
	clip.close();
}
public void resumeSong(){
	playSong= true;
}
public void turnOFSounds(){
	pauseSong();
	active= false;
}
public void startSounds(String newFile){
	Sound sound= new Sound(newFile);
}

public void beepPool(String beepFile){
	ExecutorService executorService= Executors.newFixedThreadPool(5);
	executorService.submit(new Beep(beepFile));
	executorService.shutdown();
}

public Boolean getActive() {
	return active;
}


public void setActive(Boolean active) {
	this.active = active;
}

public Thread getThread() {
	return thread;
}


public void setThread(Thread thread) {
	this.thread = thread;
}


public String getFile() {
	return file;
}


public void setFile(String file) {
	this.file = file;
}

}
